package com.yangjun.controller.businessAnalysis.memberAnalysis;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yangjun.service.bussiness.MemberAnalysisService;
@Controller
@RequestMapping("/businessAnalysis/memberAnalysis")
public class memberAnalysisController {
    private static Logger logger = Logger.getLogger(memberAnalysisController.class);
    @Autowired
    private MemberAnalysisService memberAnalysisService;
    /**
     * @Description 会员分析头部表格，柱状图，饼图数据
     * @return
     * @author yangjun
     * @Date 2016年10月25日 上午11:55:36
     */
    @ResponseBody
    @RequestMapping("/companySituation")
    public Map<String,Object> getCompanySituation(){
        return memberAnalysisService.getCompanySituation();
    }
    @ResponseBody
    @RequestMapping("/historyPlatform")
    public Map<String,Object> getHistoryPurchaseAnalysis(
            @RequestParam(value = "pageNum",required = false)String pageNum,
            @RequestParam(value = "pageSize",required = false)String pageSize){
        Map<String,Object> resultMap = Maps.newHashMap();
        Map<String,Object> dataMap = memberAnalysisService.getMemberSituation("ORDERSOURCE",null);
        List<String[]> dataList =  (List<String[]>) dataMap.get("data");
        resultMap.put("pageInfo",getListPageInfo(dataList,pageNum,pageSize));
        resultMap.put("title",dataMap.get("title"));
        resultMap.put("sum",dataMap.get("sum"));
        return resultMap;
    }
    /**
     * @Description 针对一个list作分页
     * @param dataList
     * @param pageNum
     * @param pageSize
     * @author yangjun
     * @Date 2016年10月27日 下午4:45:00
     */
    public <T> Map<String,Object> getListPageInfo(List<T> dataList,String pageNum,String pageSize){
        Map<String,Object> resultMap = Maps.newHashMap();
        List<String[]> resultList = Lists.newArrayList();
        int totalPageSize = (int)Math.ceil((float)dataList.size()/Integer.valueOf(pageSize));
        int pageNo =Integer.valueOf(pageNum);
        if(pageNo<1){
            pageNo=1;
        }else if(pageNo>totalPageSize){
            pageNo=totalPageSize;
        }
        for(int i=0,len=dataList.size();i<len;i++){
            if(i>=(pageNo-1)*Integer.valueOf(pageSize)&&i<(pageNo-1)*Integer.valueOf(pageSize)+Integer.valueOf(pageSize)){
                resultList.add((String[]) dataList.get(i));
            }
        }
        resultMap.put("totalPageSize", totalPageSize);
        resultMap.put("totalNums", dataList.size());
        resultMap.put("data", resultList);
        return resultMap;
    }

    @ResponseBody
    @RequestMapping("/historyOffices")
    public Map<String,Object> getHistoryOfficesPurchaseAnalysis(
            @RequestParam(value = "siteName",required = false)String siteName){
        return memberAnalysisService.getMemberSituation("OFFICES",siteName);
    }
    @ResponseBody
    @RequestMapping("/historyIncrease")
    public Map<String,Object> getHistoryIncrease(
            @RequestParam(value = "siteName",required = false)String siteName,
            @RequestParam(value = "officeName",required = false)String officeName){
        Map<String,Object> resultMap = Maps.newHashMap();
        resultMap.put("increase", memberAnalysisService.getAllIncreasedMember(siteName,officeName));
        resultMap.put("repurchase", memberAnalysisService.getAllRePurchaseMember(siteName,officeName));
        return resultMap;
    }

    @ResponseBody
    @RequestMapping("/historyOfficesPie")
    public Map<String,Object> getHistoryOfficesPie(
            @RequestParam(value = "siteName",required = false)String siteName){
        return memberAnalysisService.getOfficesMember(siteName);
    }

    @ResponseBody
    @RequestMapping("/newlyMember")
    public Map<String,Object> getNewlyMember(
            @RequestParam(value = "siteName",required = false)String siteName,
            @RequestParam(value = "officeName",required = false)String officeName){
        Map<String,Object> resultMap = Maps.newHashMap();
        resultMap.put("newlyMember", memberAnalysisService.getAllIncreasedMemberByMonth(siteName,officeName));
        return resultMap;
    }
    @ResponseBody
    @RequestMapping("/increaseMember")
    public Map<String,Object> getIncreaseMember(
            @RequestParam(value = "siteName",required = false)String siteName,
            @RequestParam(value = "officeName",required = false)String officeName){
        Map<String,Object> resultMap =  Maps.newHashMap();
        resultMap.put("increaseMember", memberAnalysisService.recentTwoYearIncreasedMember(siteName,officeName));
        resultMap.put("rePurchase", memberAnalysisService.recentTwoYearRePurchaseMember(siteName,officeName));
        return resultMap;
    }
    @ResponseBody
    @RequestMapping("/rePurchase")
    public Map<String,Object> getRePurchaseMember(
            @RequestParam(value = "siteName",required = false)String siteName,
            @RequestParam(value = "officeName",required = false)String officeName){
        Map<String,Object> resultMap = Maps.newHashMap();
        resultMap.put("times", memberAnalysisService.getAllRePurchaseByTimes(siteName,officeName));
        resultMap.put("money", memberAnalysisService.getAllRePurchaseByMoney(siteName,officeName));
        return resultMap;
    }
    /**
     * @Description 复购分析，根据次数查询会员数，增长，增长率，占比，分年，分月
     * @param times
     * @param flag
     * @param siteName
     * @param officeName
     * @author yangjun
     * @Date 2016年10月26日 上午11:06:01
     */
    @ResponseBody
    @RequestMapping("/rePurchaseByTimes")
    public Map<String,Object> getRePurchaseMemberByTimes(
            @RequestParam(value = "times",required = false)String times,
            @RequestParam(value = "siteName",required = false)String siteName,
            @RequestParam(value = "officeName",required = false)String officeName){
        return memberAnalysisService.getRePurchaseByTimesAndDataDate(times.split(","),siteName,officeName);
    }

    /**
     * @Description 复购分析，根据金额范围查询会员数，增长，增长率，占比，分年，分月
     * @param times
     * @param flag
     * @param siteName
     * @param officeName
     * @author yangjun
     * @Date 2016年10月26日 上午11:06:01
     */
    @ResponseBody
    @RequestMapping("/rePurchaseByMoney")
    public Map<String,Object> getRePurchaseMemberByMoney(
            @RequestParam(value = "times",required = false)String times,
            @RequestParam(value = "siteName",required = false)String siteName,
            @RequestParam(value = "officeName",required = false)String officeName){
        return memberAnalysisService.getRePurchaseByMoney(times.split(","),siteName,officeName);
    }



    /**
     * @Description 下单间隔分析
     * @param times
     * @param flag
     * @param siteName
     * @param officeName
     * @author yangjun
     * @Date 2016年10月26日 上午11:06:01
     */
    @ResponseBody
    @RequestMapping("/orderInterval")
    public Map<String,Object> getOrderInterval(
            @RequestParam(value = "siteName",required = false)String siteName,
            @RequestParam(value = "officeName",required = false)String officeName){
        Map<String,Object> resultMap = Maps.newHashMap();
        resultMap.put("orderInterval", memberAnalysisService.getHistoryOrdersByOrderInterval(siteName,officeName));
        return resultMap;
    }

    /**
     * @Description 下单间隔分析，根据月份范围查询订单数，增长，增长率，占比，分年，分月
     * @param times
     * @param flag
     * @param siteName
     * @param officeName
     * @author yangjun
     * @Date 2016年10月26日 上午11:06:01
     */
    @ResponseBody
    @RequestMapping("/orderIntervalByMonth")
    public Map<String,Object> getOrderIntervalByMonth(
            @RequestParam(value = "months",required = false)String months,
            @RequestParam(value = "siteName",required = false)String siteName,
            @RequestParam(value = "officeName",required = false)String officeName){
        return memberAnalysisService.getOrdersByOrderInterval(months.split(","),siteName,officeName);
    }

    /**
     * @Description 最后购买分析(表格)
     * @param times
     * @param flag
     * @param siteName
     * @param officeName
     * @author yangjun
     * @Date 2016年10月26日 上午11:06:01
     */
    @ResponseBody
    @RequestMapping("/lastPurchase")
    public Map<String,Object> getLastPurchase(
            @RequestParam(value = "siteName",required = false)String siteName,
            @RequestParam(value = "officeName",required = false)String officeName){
        Map<String,Object> resultMap =Maps.newHashMap();
        resultMap.put("lastPurchase", memberAnalysisService.getLastPurchaseMembersByOrderInterval(siteName,officeName));
        return resultMap;
    }


    @ResponseBody
    @RequestMapping("/lastPurchaseSnapshot")
    public Map<String,Object> getLastPurchaseSnapshot(
            @RequestParam(value = "months",required = false)String months,
            @RequestParam(value = "siteName",required = false)String siteName,
            @RequestParam(value = "officeName",required = false)String officeName){
        return memberAnalysisService.getLastPurchaseSnapshot(months.split(","),siteName,officeName);
    }
}
